package com.example.yls.inote.adapter;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.yls.inote.R;
import com.example.yls.inote.data.DataStore;
import com.example.yls.inote.models.Note;
import com.example.yls.inote.utils.DateUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class NotesAdapter extends RecyclerView.Adapter<NotesAdapter.NotesViewHolder> {

    private final Context context;
    private List<Note> notes = new ArrayList<>();
    private boolean isRefreshing = false;

    public NotesAdapter(Context context) {
        this.context = context;
        setHasStableIds(true);
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        refresh();
    }

    @Override
    public long getItemId(int position) {
        return notes.get(position).getId();
    }

    @Override
    public int getItemCount() {
        return notes.size();
    }

    @Override
    public NotesViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_note, parent, false);
        return new NotesViewHolder(view);
    }

    @Override
    public void onBindViewHolder(NotesViewHolder holder, int position) {
        final Note note = notes.get(position);
        holder.titleText.setText(note.getTitle());
        holder.bodyText.setText(note.getBody());
        holder.createAtText.setText(getCreatedTime(note.getCreatedAt()));
    }

    public void refresh() {
        if (isRefreshing) return;
        isRefreshing = true;
        DataStore.execute(new Runnable() {
            @Override
            public void run() {
                final List<Note> notes = DataStore.getNotes().getAll();
                new Handler(Looper.getMainLooper()).post(new Runnable() {
                    @Override
                    public void run() {
                        NotesAdapter.this.notes = notes;
                        notifyDataSetChanged();
                        isRefreshing = false;
                    }
                });
            }
        });
    }

    public String getCreatedTime(long updatedTime) {
        return setNoteTime(updatedTime);
    }

    public static class NotesViewHolder extends RecyclerView.ViewHolder {

        TextView titleText;
        TextView bodyText;
        TextView createAtText;


        NotesViewHolder(View itemView) {
            super(itemView);
            titleText = itemView.findViewById(R.id.tv_title);
            bodyText = itemView.findViewById(R.id.tv_body);
            createAtText = itemView.findViewById(R.id.tv_create_at);

        }
    }
    private String setNoteTime(long time) {

        // 系统当前时间，用于与便签的修改时间进行对比
        long nowTime = System.currentTimeMillis();

        if (DateUtils.isInSameDay(nowTime, time))  // 同一天
            return setNoteTimeInfo(time,new SimpleDateFormat("HH:mm"));
        else if (DateUtils.isInSameYear(nowTime, time))  // 同一年
            return setNoteTimeInfo(time, new SimpleDateFormat("MM-dd HH:mm"));
        else // 其他
            return setNoteTimeInfo(time, new SimpleDateFormat("yyyy-MM-dd HH:mm"));
    }

    private String setNoteTimeInfo(long time, SimpleDateFormat simpleDateFormat) {
        return  simpleDateFormat.format(new Date(time));
    }

}
